<template>
  <div>
    <el-dialog title="线上笔试" :visible.sync="dialogVisible" width="60%">
      <div>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">收件人：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="email" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">抄送：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="model.cc" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">主题：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="draft_subject" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">附件：</div>
          </el-col>
          <el-col :span="20">
            <el-checkbox-group v-model="attachmentChecklist" style="margin-top:0.5rem">
              <el-checkbox v-for="item of attachmentoptions" :key="item.id" :label="item.id">{{item.name}}</el-checkbox>
            </el-checkbox-group>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">正文：</div>
          </el-col>
          <el-col :span="20">
            <el-input type="textarea" placeholder="请输入内容" v-model="content" rows="15">
            </el-input>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doforward" v-loading.fullscreen.lock="fullscreenLoading">发送给：{{data.name}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import _ from 'lodash'
  export default {
    data() {
      return {
        dialogVisible: false,
        fullscreenLoading: false,
        data: {},
        q: {
          to: [],
          cc: [],
          subject: "",
          content: "",
          resumeId: "",
          attachments:[]
        },
        email:"",
        draft_subject: "",
        content:"",
        model: {},
        attachmentoptions: [],
        modelData: [],
        attachmentChecklist: [],
      }
    },
    methods: {
      init(data) {
        this.dialogVisible = true
        this.data = data
        this.q.resumeId = this.data.resumeId
        this.getwrittenExaminationInvitation()
        this.getAttachment()
      },
      handleReceiver(val) {
        return val.split(",")
      },
      getAttachment() {
        this.$http({
          url: `${this.$http.adornUrl('/attachment/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          if (data.data.code == 0) {
            this.attachmentoptions = data.data.list
          } else {
            this.$message({
              message: '获取附件列表失败',
              type: 'error'
            });
          }
        })
      },
      doforward() {
        this.fullscreenLoading = true
        this.q.to = this.handleReceiver(this.email)
        this.q.cc = this.handleReceiver(this.model.cc)
        this.q.subject = this.draft_subject
        this.q.content = this.content
        this.q.attachments = this.attachmentChecklist
        this.$http({
          url: `${this.$http.adornUrl('/resume/writtenExaminationInvitation')}`,
          method: 'post',
          data: this.$http.adornData(this.q, false)
        }).then(({
          data
        }) => {
          if (data['code'] == 0) {
            this.dialogVisible = false
            this.$message({
              message: '发送邮件成功',
              type: 'success'
            });
            this.$emit('fnc')
            this.fullscreenLoading = false
          } else {
            this.$message({
              message: '发送邮件错误',
              type: 'error'
            });
            this.fullscreenLoading = false
          }
        })

      },
      getwrittenExaminationInvitation() {
        let that = this
        this.$http({
          url: `${this.$http.adornUrl('/emailDict/writtenExaminationInvitation?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          console.log("返回的笔试模板数据")
          console.log(data.data.writtenExaminationInvitation)
          that.model = data.data.writtenExaminationInvitation
          that.attachmentChecklist = JSON.parse(data.data.writtenExaminationInvitation.attachment)
          let prefix = data.data.writtenExaminationInvitation.subjectPrefix
          let email = data.data.writtenExaminationInvitation.sendTo
          let content = data.data.writtenExaminationInvitation.content
          for (let key in that.data) {
            //遍历对象属性值
            prefix = prefix.replace("${"+key+"}",that.data[key])
            email = email.replace("${"+key+"}",that.data[key])
            content = content.replace("${"+key+"}",that.data[key])
          }
          that.draft_subject = prefix
          that.email = email
          that.content = content
        })
      }


    }

  }

</script>
